package com.ihr360.expr.function.excel;


import com.ihr360.expr.ExprDouble;
import com.ihr360.expr.ExprError;
import com.ihr360.expr.ExprException;
import com.ihr360.expr.context.IEvaluationContext;
import com.ihr360.expr.core.AbstractFunction;
import com.ihr360.expr.core.Expr;
import com.ihr360.expr.core.ExprNumber;
import com.ihr360.expr.utils.ExcelDate;
import com.ihr360.expr.utils.ExprUtils;


/**
 * Excel的时间算法，需要修改
 */
public class DATE extends AbstractFunction
{
    @Override
    public Expr evaluate(IEvaluationContext context, Expr[] args)
            throws ExprException {
        assertArgCount(args, 3);
        Expr eY = ExprUtils.evalArg(context, args[0]);
        if (!isNumber(eY)){
            return ExprError.VALUE;
        }
        double y = ((ExprNumber) eY).doubleValue();
        Expr eM = ExprUtils.evalArg(context, args[1]);
        if (!isNumber(eM))
        {
            return ExprError.VALUE;
        }
        double m = ((ExprNumber) eM).doubleValue();
        Expr eD = ExprUtils.evalArg(context, args[1]);
        if (!isNumber(eD))
        {
            return ExprError.VALUE;
        }
        double d = ((ExprNumber) eD).doubleValue();
        double r = ExcelDate.date(y, m, d);
        if (r < 0){
            return ExprError.NUM;
        }
        return new ExprDouble(r);
    }

}
